import 'package:animate_do/animate_do.dart';
import 'package:flutter/material.dart';

/// 顶部导航栏
/// https://www.bilibili.com/video/BV1HA41167Nw/?spm_id_from=333.788&vd_source=e07cc8cc94eb22bd76722071b03cd199

class AppBarWidget extends StatelessWidget implements PreferredSizeWidget {
  AppBarWidget({
    super.key,
    this.backgroundColor,
    this.elevation,
    this.leading,
    this.actions,
    this.automaticallyImplyLeading = true,
    this.isAnimated,
    this.isShow,
    this.title,
  });

  /// 用于应用栏【材质】的填充颜色
  final Color? backgroundColor;

  /// 此属性控制应用栏下方阴影的大小【shadowColor】不为空
  final double? elevation;

  /// 在工具栏的【title】 之前显示的小部件
  final Widget? leading;

  /// 在【title】小部件之后显示在一行中的小部件列表
  final List<Widget>? actions;

  // 是否展示返回按钮
  bool automaticallyImplyLeading = true;

  // 是否使用动画
  final bool? isAnimated;

  // 是否展示
  final bool? isShow;

  // 标题组件
  final Widget? title;

  @override
  Size get preferredSize => const Size.fromHeight(30);

  @override
  Widget build(BuildContext context) {
    return _mainView();
  }

  Widget _mainView() {
    // return AppBar(
    //   backgroundColor: backgroundColor ?? Colors.transparent,
    //   elevation: elevation ?? 0,
    //   leading: leading,
    //   actions: actions,
    //   title: title,
    //   automaticallyImplyLeading: automaticallyImplyLeading,
    // );

    var appBar = AppBar(
      backgroundColor: backgroundColor ?? Colors.transparent,
      elevation: elevation ?? 0,
      leading: leading,
      actions: actions,
      title: title,
      automaticallyImplyLeading: automaticallyImplyLeading,
    );

    return isAnimated == true
        ? isShow == true
            ? FadeInDown(
                duration: Duration(
                  milliseconds: 300,
                ),
                child: appBar,
              )
            : FadeOutUp(
              animate: true,
                duration: Duration(
                  milliseconds: 300,
                ),
                child: appBar,
              )
        : appBar;
  }
}

/// 状态栏
/// PreferredSizeWidget 表示可变尺寸的组件
class SlideAppBarWidget extends StatelessWidget implements PreferredSizeWidget {
  const SlideAppBarWidget(
      {super.key,
      required this.child,
      required this.controller,
      required this.visible});

  // 子组件
  final PreferredSizeWidget child;

  // 动画控制器
  final AnimationController controller;

  // 是否可见
  final bool visible;

  @override
  Size get preferredSize => child.preferredSize;

  @override
  Widget build(BuildContext context) {
    visible ? controller.reverse() : controller.forward();
    return SlideTransition(
      position: Tween<Offset>(
        begin: Offset.zero,
        end: Offset(
          0,
          -1,
        ),
      ).animate(
        // 指定动画的行为，例如：先快后慢
        CurvedAnimation(
          parent: controller,
          curve: Curves.fastOutSlowIn,
        ),
      ),
      child: child,
    );
  }
}
